#include "appgreport.h"
#include "ui_appgreport.h"
#include "appgreport.h"
#include"QPainter"
#include "qsettings.h"
#include "HRVPro/hrvdataprocess.h"
#include"Config/constant.h"

APPGReport::APPGReport(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::APPGReport)
{
    ui->setupUi(this);
    ui->checkBox->setChecked(false);
    ui->checkBox_2->setChecked(false);
    ui->checkBox_3->setChecked(false);
    ui->checkBox_4->setChecked(false);
    ui->checkBox_5->setChecked(false);
    ui->checkBox_6->setChecked(false);
    ui->progressBar_CO->setTextVisible(false);
    ui->progressBar_PWV->setTextVisible(false);
    ui->progressBar_ReBlood->setTextVisible(false);
    ui->progressBar_diffPPG->setTextVisible(false);

    Series_ppg = new QLineSeries;
    xAxis_ppg = new QValueAxis();
    yAxis_ppg = new QValueAxis();
    Series_Appg = new QLineSeries;
    xAxis_appg = new QValueAxis();
    yAxis_appg = new QValueAxis();

    QSettings setting("./Configuration.ini",QSettings::IniFormat);
    setting.beginGroup("Config");
    QString reportName=setting.value("ReportName").toString();
    setting.endGroup();
    ui->label_Vascular_Title->setText(reportName+"（末梢血液循环）分析报告");

}

APPGReport::~APPGReport()
{
    delete Series_ppg;
    delete xAxis_ppg;
    delete yAxis_ppg;
    delete Series_Appg;
    delete xAxis_appg;
    delete yAxis_appg;
    delete ui;
}

void APPGReport::setValue(DataBeans dataBean)
{
    QSettings *setting=new QSettings("./Configuration.ini",QSettings::IniFormat);
    setting->beginGroup("Config");
    QString JGName=setting->value("User").toString();
    setting->endGroup();
    delete setting;
    HRVDataProcess hrvdataPro;
    HRVIndex hi=hrvdataPro.Json2HRVData(dataBean.hrvIndexResults);
    ui->label_DW->setText(JGName);
    ui->label_display_ID->setText(dataBean.UserID);
    ui->label_display_name->setText(dataBean.UserName);
    ui->label_display_age->setText(dataBean.UserAge+"/"+dataBean.UserSex);
    ui->label_display_time->setText(dataBean.MeasureTime);


    InitialAxis_Chart_PPG(hi.appg.Pre_PPG);
    InitialAxis_Chart_APPG(hi.appg.Pre_APPG);
    chart_ppg=CreateLineChart_PPG();
    chart_Appg=CreateLineChart_APPG();
    ui->widget_ppg_report->setChart(chart_ppg);
    ui->widget_ppg_report->setRenderHint(QPainter::Antialiasing,true);
    setPPGData(hi.appg.Pre_PPG);

    ui->widget_appg_report->setChart(chart_Appg);
    ui->widget_appg_report->setRenderHint(QPainter::Antialiasing,true);
    setAPPGData(hi.appg.Pre_APPG);

    int vs=hi.appg.VascularState;//血管阶段
    if(vs==0)
    {
        ui->checkBox->setChecked(true);
    }else if(vs==1)
    {
        ui->checkBox_2->setChecked(true);
    }else if(vs==2)
    {
        ui->checkBox_3->setChecked(true);
    }else if(vs==3)
    {
        ui->checkBox_4->setChecked(true);
    }else if(vs==4)
    {
        ui->checkBox_5->setChecked(true);
    }else if(vs==5)
    {
        ui->checkBox_6->setChecked(true);
    }

    QList<double>range_DPI,range_SP,range_BVT,range_RBV;
    range_DPI.append(0);
    range_DPI.append(-20);
    range_DPI.append(-120);
    range_DPI.append(-150);

    range_SP.append(0);
    range_SP.append(-20);
    range_SP.append(-50);
    range_SP.append(-150);

    range_BVT.append(0);
    range_BVT.append(-2);
    range_BVT.append(-13);
    range_BVT.append(-50);

    range_RBV.append(-50);
    range_RBV.append(-30);
    range_RBV.append(-5);
    range_RBV.append(0);
    ui->progressBar_diffPPG->setValue(getIndexLen(range_DPI,hi.appg.DPI,false));
    ui->progressBar_CO->setValue(getIndexLen(range_SP,hi.appg.SP,false));
    if(hi.appg.BVT>0){
        ui->progressBar_PWV->setValue(getIndexLen(range_BVT,-1*hi.appg.BVT,false));
        qDebug()<<"血管弹性-1"<<getIndexLen(range_DPI,-1*hi.appg.BVT,false);
    }
    else{
        ui->progressBar_PWV->setValue(getIndexLen(range_BVT,hi.appg.BVT,false));
        qDebug()<<"血管弹性"<<getIndexLen(range_DPI,hi.appg.BVT,false);
    }

    ui->progressBar_ReBlood->setValue(getIndexLen(range_RBV,hi.appg.RBV,true));
    ui->label_diffPPGV->setText(QString::number(hi.appg.DPI,'f',2));
    ui->label_COV->setText(QString::number(hi.appg.SP,'f',2));
    ui->label_Vascular_PwvV->setText(QString::number(hi.appg.BVT,'f',2));
    ui->label_ReBloodV->setText(QString::number(hi.appg.RBV,'f',2));

    ui->label_Re_VascularAgeV->setText(QString::number(hi.appg.Age));
    ui->label_ReKV->setText(QString::number(hi.appg.K,'f',2));
    ui->textEdit_vascular_res->setText(hi.appg.Vascular_res);

}
int APPGReport::getIndexLen(QList<double>ranger, double indexv,bool flag){
    //flag=true,范围从小变大，否则是从大变小
    double a=ranger.at(0);
    double b=ranger.at(1);
    double c=ranger.at(2);
    double d=ranger.at(3);
    int len=0;
    if(flag){
        if(indexv<a)
            indexv=a;
        if(indexv>d)
            indexv=d;
        if(indexv>=a && indexv<b){
            len=150-150*(b-indexv)/(b-a);
        }else if(indexv>=b && indexv<c){
            len=300-150*(c-indexv)/(c-b);
        }else{
            len=450-150*(d-indexv)/(d-c);
        }

    }else{
        if(indexv>a)
            indexv=a;
        if(indexv<d)
            indexv=d;
        if(indexv<=a && indexv>b){
            len=150-150*(b-indexv)/(b-a);
        }else if(indexv<=b && indexv>c){
            len=300-150*(c-indexv)/(c-b);
        }else{
            len=450-150*(d-indexv)/(d-c);
        }
    }

    return len;

}
void APPGReport::InitialAxis_Chart_PPG(QList<double>ppg)
{
    int rr_Len=ppg.size();
    xAxis_ppg->setRange(0, rr_Len);
    double max=ppg.at(0);
    double min=ppg.at(0);

    for(int i=0;i<rr_Len;i++)
    {
        if(max<ppg.at(i))
            max=ppg.at(i);
        if(min>ppg.at(i))
            min=ppg.at(i);
    }
    yAxis_ppg->setRange(min,max);
    qDebug()<<"InitialAxis_Chart_PPG,min="<<min<<",max="<<max;
    xAxis_ppg->setLabelsFont( QFont("微软雅黑", 12, QFont::Normal, false));
    yAxis_ppg->setLabelsFont( QFont("微软雅黑", 12, QFont::Normal, false));
    yAxis_ppg->setTitleText("脉搏波");
    yAxis_ppg->setTitleBrush(QBrush(QColor(0x2E,0x5A,0x82),Qt::SolidPattern));
    yAxis_ppg->setLabelsColor(QColor(0x2E,0x5A,0x82));
    yAxis_ppg->setGridLineVisible(false);
    yAxis_ppg->setGridLineVisible(true);
    yAxis_ppg->setGridLineColor(QColor(0xC4,0x91,0x44,0x20));
    xAxis_ppg->setLineVisible(false);
    xAxis_ppg->setVisible(false);
    yAxis_ppg->setTitleText("mv)");
    yAxis_ppg->setVisible(true);
}
QChart* APPGReport::CreateLineChart_PPG()
{
    QChart *chart = new QChart();
    Series_ppg->setPen(QPen(QColor(0x00,0x00,0x00),2));//设置波形颜色
    chart->addSeries(Series_ppg);
    chart->setBackgroundVisible(true);
    chart->legend()->hide(); /* hide legend */
    chart->addAxis(xAxis_ppg, Qt::AlignBottom);
    chart->addAxis(yAxis_ppg, Qt::AlignLeft);
    Series_ppg->attachAxis(xAxis_ppg);
    Series_ppg->attachAxis(yAxis_ppg);
    return chart;
}

void APPGReport::setPPGData(QList<double>ppg)
{

    Series_ppg->clear();
    int rr_Len=ppg.size();
    xAxis_ppg->setRange(0, rr_Len);
    for(int i=0;i<rr_Len;i++)
    {

        Series_ppg->append(QPointF(i,ppg.at(i)));
    }

}
void APPGReport::InitialAxis_Chart_APPG(QList<double>appg)
{
//    yAxis_appg->setRange(0, 1200);
//    xAxis_appg->setRange(0, 128);
    int rr_Len=appg.size();
    xAxis_appg->setRange(0, rr_Len);
    double max=appg.at(0);
    double min=appg.at(0);

    for(int i=0;i<rr_Len;i++)
    {
        if(max<appg.at(i))
            max=appg.at(i);
        if(min>appg.at(i))
            min=appg.at(i);
    }
    yAxis_appg->setRange(min, max);
    qDebug()<<"InitialAxis_Chart_APPG,min="<<min<<",max="<<max;
    xAxis_appg->setLabelsFont( QFont("微软雅黑", 12, QFont::Normal, false));
    yAxis_appg->setLabelsFont( QFont("微软雅黑", 12, QFont::Normal, false));
    yAxis_appg->setTitleText("加速度脉搏波");
    yAxis_appg->setTitleBrush(QBrush(QColor(0x2E,0x5A,0x82),Qt::SolidPattern));
    yAxis_appg->setLabelsColor(QColor(0x2E,0x5A,0x82));
    yAxis_appg->setGridLineVisible(false);
    yAxis_appg->setGridLineVisible(true);
    yAxis_appg->setGridLineColor(QColor(0xC4,0x91,0x44,0x20));
    xAxis_appg->setLineVisible(false);
    xAxis_appg->setVisible(false);
    yAxis_appg->setTitleText("mv");
    yAxis_appg->setVisible(true);
}
QChart* APPGReport::CreateLineChart_APPG()
{
    QChart *chart = new QChart();
    Series_Appg->setPen(QPen(QColor(0x00,0x00,0x00),2));//设置波形颜色
    chart->addSeries(Series_Appg);
    chart->setBackgroundVisible(true);
    chart->legend()->hide(); /* hide legend */
    chart->addAxis(xAxis_appg, Qt::AlignBottom);
    chart->addAxis(yAxis_appg, Qt::AlignLeft);
    Series_Appg->attachAxis(xAxis_appg);
    Series_Appg->attachAxis(yAxis_appg);
    return chart;
}

void APPGReport::setAPPGData(QList<double>appg)
{
    int len=appg.size();
    Series_Appg->clear();
    for(int i=0;i<len;i++)
    {
        Series_Appg->append(QPointF(i,appg.at(i)));
    }
}
